﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class MCSQBControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        MPPersistence Persistence1;
        TextBox txtID, txtROLEID,txt密钥编号;
        DropDownList DropDownList1, dll所属项目, ddl用户类型;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid)) { }
            else
            {
                base.Page.ClientScript.RegisterStartupScript(this.GetType(), "ss", "<script>distr1()</script>");
            }
            Persistence1 = (MPPersistence)FindControl("Persistence1");
            txtID = (TextBox)FindControl("txtID");
            txtROLEID = (TextBox)FindControl("txtROLEID");
            txt密钥编号 = (TextBox)FindControl("txt密钥");
            DropDownList1 = (DropDownList)FindControl("DropDownList1");

            dll所属项目 = (DropDownList)FindControl("ddlPID");
            ddl用户类型 = (DropDownList)FindControl("ddl用户类型");
            Panel panel1 = (Panel)FindControl("panel1");
            if (!base.Page.IsPostBack)
            {
                BindDll();
                BindDll1();
                BindRoleId();
                Persistence1.Table = "ZS_MCYHCQB";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["ID"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();
                
                
            }
            Persistence1.BindControl = panel1;
            
            Button btn_Insert = (Button)FindControl("btn_Insert");
            btn_Insert.Click += new EventHandler(btnSave_Click);
            DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
            Button Button2 = (Button)FindControl("Button2");
            Button2.Click += new EventHandler(Button2_Click);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            BindDll1();
        }
        
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string strid = base.Page.Request["ID"];
            if (string.IsNullOrEmpty(strid)) { txtID.Text = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper(); } else { txtID.Text = strid; }
        }
        protected void BindRoleId() {
            if (!string.IsNullOrEmpty(userid))
            {
                string strRoleId = string.Empty;
                if (!_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
                {
                    strRoleId = _EluZSDataProvider.GetString("Select t.id From zs_role t,zs_user_in_role t1 Where t.id=t1.roleid And t.rolename Like '%征收部门%' And t1.userid='" + userid + "'");
                }
                else
                {
                    strRoleId = DropDownList1.SelectedValue;
                }
                if (!string.IsNullOrEmpty(strRoleId))
                {
                    string strSX = _EluZSDataProvider.GetString("select sx from zs_role Where id='" + strRoleId + "'");
                    string strMaxNo = _EluZSDataProvider.GetString(@"Select (Case When length(to_char(nvl(max(to_number(nvl(substr(密钥,3,5),0))),0)+1))=1 Then '000'||to_char(nvl(max(to_number(nvl(substr(密钥,3,5),0))),0)+1)
When length(to_char(nvl(max(to_number(nvl(substr(密钥,3,5),0))),0)+1))=2 Then '00'||to_char(nvl(max(to_number(nvl(substr(密钥,3,5),0))),0)+1)
When length(to_char(nvl(max(to_number(nvl(substr(密钥,3,5),0))),0)+1))=3 Then '0'||to_char(nvl(max(to_number(nvl(substr(密钥,3,5),0))),0)+1)
When length(to_char(nvl(max(to_number(nvl(substr(密钥,3,5),0))),0)+1))=4 Then to_char(nvl(max(to_number(nvl(substr(密钥,3,5),0))),0)+1)
Else '000'||to_char(nvl(max(to_number(nvl(substr(密钥,3,5),0))),0)+1) End)
 From ZS_MCYHCQB t Where t.ROLEID ='" + strRoleId + "'");
                    
                    string strid = base.Page.Request["ID"];
                    if (!string.IsNullOrEmpty(strid))
                    {
                        string nowMY = _EluZSDataProvider.GetString("Select 密钥 From ZS_MCYHCQB Where Id='" + strid + "'");
                        if (string.IsNullOrEmpty(nowMY))
                        {
                            txt密钥编号.Text = strSX + strMaxNo;
                            txtROLEID.Text = strRoleId;
                        }
                        else {
                            txt密钥编号.Text = nowMY;
                            txtROLEID.Text = _EluZSDataProvider.GetString("Select roleid From ZS_MCYHCQB Where Id='" + strid + "'");
                        }
                    }
                    else {
                        txt密钥编号.Text = strSX + strMaxNo;
                        txtROLEID.Text = strRoleId;
                    }
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strNowMY = txt密钥编号.Text.Trim();
            
            if (!string.IsNullOrEmpty(strNowMY)) {
                string strid = base.Page.Request["ID"];
                if (!string.IsNullOrEmpty(strid))
                {
                    if (Convert.ToInt32(_EluZSDataProvider.GetString("Select count(ID) From ZS_MCYHCQB Where Id not in ('" + strid + "') and 密钥='" + strNowMY + "'")) > 0)
                    {
                        base.Page.ClientScript.RegisterStartupScript(this.GetType(), "wfwe", "<script>alert('该密钥编号已存在！');</script>");
                        return;
                    }
                }
            }
            Persistence1.Update();
            HttpContext.Current.Response.Redirect("MCSQList.aspx?roleid="+base.Page.Request["roleid"]+"&type=" + base.Page.Request["type"]);
        }
        protected void BindDll() {
            DropDownList1.DataSource = _EluZSDataProvider.GetTable("Select Id,rolename From zs_role Where rolename Like '%征收部门%'");
            DropDownList1.DataTextField = "rolename";
            DropDownList1.DataValueField = "Id";
            DropDownList1.DataBind();
            string strid = base.Page.Request["ID"];
            if (!string.IsNullOrEmpty(strid)) {
                DropDownList1.SelectedValue = _EluZSDataProvider.GetString("Select ROLEID From ZS_MCYHCQB Where Id='" + strid + "'");
            }
        }
        protected void BindDll1()
        {
            DataTable dt = new DataTable();
            string strWhere = string.Empty;
            TextBox TextBox1 = (TextBox)FindControl("TextBox1");
            if (!string.IsNullOrEmpty(TextBox1.Text.Trim()))
            {
                strWhere = " and 项目名称 like '%" + TextBox1.Text.Trim() + "%'";
            }
            if (!_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
            {
                string strRoleName = _EluZSDataProvider.GetString("Select substr(t.rolename,0,2) From zs_role t,zs_user_in_role t1 Where t.id=t1.roleid And t.rolename Like '%征收部门%' And t1.userid='" + userid + "'");
                dt = _EluZSDataProvider.GetTable("Select 项目名称,ID From zs_project where 区属 like '%" + strRoleName + "%'" + strWhere);
            }
            else
            {
                dt = _EluZSDataProvider.GetTable("Select 项目名称,ID From zs_project where 1=1 " + strWhere);
            }
            DataRow dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            dll所属项目.DataSource = dt;
            dll所属项目.DataTextField = "项目名称";
            dll所属项目.DataValueField = "Id";
            dll所属项目.DataBind();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindRoleId();
        }
        //DataBind();
        //Button btn_save = (Button)FindControl("btn_save");
        //btn_save.Click += new EventHandler(btn_save_Click);
        //Button btn_fh = (Button)FindControl("btn_fh");
        //btn_fh.Click += new EventHandler(btn_fh_Click);
//        protected void btn_fh_Click(object sender, EventArgs e)
//        {
//            if (Page.Request["ID"] != null)
//            {
//                Page.ClientScript.RegisterStartupScript(this.GetType(), "s2", "<script>location.href = 'MCSQList.aspx';</script>");
//            }
//            else
//            {
//                Page.ClientScript.RegisterStartupScript(this.GetType(), "s1", "<script>location.href = 'MCSQ.aspx';</script>");
//            }
//        }
//        protected void btn_save_Click(object sender, EventArgs e)
//        {
//            DropDownList dll所属项目 = (DropDownList)FindControl("dll所属项目");
//            TextBox txt申请人姓名 = (TextBox)FindControl("txt申请人姓名");
//            TextBox txt职务 = (TextBox)FindControl("txt职务");
//            TextBox txt权限范围 = (TextBox)FindControl("txt权限范围");
//            TextBox txt备注 = (TextBox)FindControl("txt备注");
//            TextBox txt联系电话 = (TextBox)FindControl("txt联系电话");
//            TextBox txt登录名 = (TextBox)FindControl("txt登录名");
//            DropDownList dll用户类型 = (DropDownList)FindControl("dll用户类型");
//            TextBox txt密钥编号 = (TextBox)FindControl("txt密钥编号");
//            string qs = string.Empty;
//            #region 判断区属
//            if (Page.Request["qs"]=="bx")
//            {
//                qs = "白下区";
//            }
//            if (Page.Request["qs"] == "gl")
//            {
//                qs = "鼓楼区";
//            }
//            if (Page.Request["qs"] == "xw")
//            {
//                qs = "玄武区";
//            }
//            if (Page.Request["qs"] == "jn")
//            {
//                qs = "江宁区";
//            }
//            if (Page.Request["qs"] == "lh")
//            {
//                qs = "六合区";
//            }
//            if (Page.Request["qs"] == "ls")
//            {
//                qs = "溧水县";
//            }
//            if (Page.Request["qs"] == "gc")
//            {
//                qs = "高淳县";
//            }
//            if (Page.Request["qs"] == "jy")
//            {
//                qs = "建业区";
//            }
//            if (Page.Request["qs"] == "qh")
//            {
//                qs = "秦淮区";
//            }
//            if (Page.Request["qs"] == "xg")
//            {
//                qs = "下关区";
//            }
//            if (Page.Request["qs"] == "xx")
//            {
//                qs = "栖霞区";
//            }
//            if (Page.Request["qs"] == "yht")
//            {
//                qs = "雨花台区";
//            }
//            if (Page.Request["qs"] == "pk")
//            {
//                qs = "浦口区";
//            }
//            #endregion
//            if (Page.Request["ID"]!=null)
//            {
//                string sql1 = "Select * from zs_user where loginid ='" + txt登录名.Text.Trim() + "' ";
//                DataTable dt = _EluZSDataProvider.GetTable(sql1);
//                if (dt.Rows.Count > 0)
//                {
//                    Page.ClientScript.RegisterStartupScript(this.GetType(), "s1", "<script>alert('该登录名已被使用！请重新输入');location.href = window.location.href;</script>");
//                    return;
//                }
//                else
//                {
//                    string sql = "update zs_mcyhcqb set 所属项目 = '" + dll所属项目.SelectedValue + "', 申请人姓名 = '" + txt申请人姓名.Text.Trim() + "', 职务 = '" + txt职务.Text.Trim() + "', 权限范围 = '" + txt权限范围.Text.Trim() + "', 备注 = '" + txt备注.Text.Trim() + "', 用户类型 = '" + dll用户类型.SelectedValue + "', 密钥编号 = '" + txt密钥编号.Text.Trim() + "', 项目名称 = '" + dll所属项目.Items[dll所属项目.SelectedIndex].Text + "', 登录名 = '" + txt登录名.Text + "' where id='" + Page.Request["ID"] + "' ";
//                    string num = _EluZSDataProvider.GetString(sql);
//                    string sql2 = "update zs_user set name='" + txt申请人姓名.Text.Trim() + "', pwd = '" + txt密钥编号.Text.Trim() + "' where loginid = '" + txt登录名.Text + "' ";
//                    string num1 = _EluZSDataProvider.GetString(sql);
//                    Page.ClientScript.RegisterStartupScript(this.GetType(), "s1", "<script>alert('修改成功');location.href = 'MCSQList.aspx';</script>");

//                }
                
//            }
//            else
//            {
//                string sql1 = "Select * from zs_user where loginid ='"+txt登录名.Text.Trim()+"' ";
//                DataTable dt = _EluZSDataProvider.GetTable(sql1);
//                if (dt.Rows.Count>0)
//                {
//                    Page.ClientScript.RegisterStartupScript(this.GetType(), "s1", "<script>alert('该登录名已被使用！请重新输入');location.href = window.location.href;</script>");
//                    return;
//                }
//                else
//                {
//                    string sql = "insert into zs_mcyhcqb values(sys_guid(),'" + dll所属项目.SelectedValue + "','" + txt申请人姓名.Text.Trim() + "','" + txt职务.Text.Trim() + "','" + txt权限范围.Text.Trim() + "','" + txt备注.Text.Trim() + "','" + txt联系电话.Text.Trim() + "','" + dll用户类型.SelectedValue + "','" + txt密钥编号.Text.Trim() + "','" + qs + "','" + dll所属项目.Items[dll所属项目.SelectedIndex].Text + "','" + txt登录名.Text + "')";
//                    string num = _EluZSDataProvider.GetString(sql);
//                    string sql2 = @"Insert Into Zs_User Values (sys_guid() , '" + txt登录名.Text.Trim() + "', '" + txt申请人姓名.Text.Trim() + "', '" + txt密钥编号.Text.Trim() + "', '', '', '', '',  '', '', '', '','', '','', '', '', '')";
//                    string num1 = _EluZSDataProvider.GetString(sql2);
//                    Page.ClientScript.RegisterStartupScript(this.GetType(), "s1", "<script>alert('保存成功');location.href = 'MCSQ.aspx';</script>");
//                }
                
//            }
           
           
//        }
//        public override void DataBind()
//        {
//            string userid = string.Empty;
//            if (Page.Session["UserID"] != null)
//            {
//                userid = Page.Session["UserID"].ToString();
//            }
//            string sql1 = @"Select Rolename
//  From Zs_Role Where Id = (Select Roleid  From Zs_User_In_Role Where Userid = '" + userid + "')";
//            string Rname = _EluZSDataProvider.GetString(sql1);
//            string rolename = string.Empty;
//            string sql = string.Empty;
//            if (!Rname.Equals("超级管理员"))
//            {
//                rolename = Rname.Substring(0, 2);
//                sql = "select ID,项目名称 from zs_project where 1=1 and 区属 like'%" + rolename + "%' ";
//            }
//            else
//            {
//                sql = "select ID,项目名称 from zs_project where 1=1 ";
//            }
//            DataTable dt = _EluZSDataProvider.GetTable(sql);
//            DropDownList dll所属项目 = (DropDownList)FindControl("dll所属项目");
//            if (dt.Rows.Count > 0)
//            {
//                for (int i = 0; i < dt.Rows.Count; i++)
//                {
//                    dll所属项目.Items.Add(new ListItem(dt.Rows[i]["项目名称"].ToString(), dt.Rows[i]["ID"].ToString()));
//                }
//            }
//            dll所属项目.Items.Insert(0, new ListItem("", ""));
//            if (Page.Request["ID"] != null)
//            {
//                string sql2 = "Select * from zs_mcyhcqb where id = '" + Page.Request["ID"] + "'";
//                DataTable dt1 = _EluZSDataProvider.GetDTTable(sql2);
//                dll所属项目.SelectedValue = dt1.Rows[0]["所属项目"].ToString();
//                TextBox txt申请人姓名 = (TextBox)FindControl("txt申请人姓名");
//                txt申请人姓名.Text = dt1.Rows[0]["申请人姓名"].ToString();
//                TextBox txt职务 = (TextBox)FindControl("txt职务");
//                txt职务.Text = dt1.Rows[0]["职务"].ToString();
//                TextBox txt权限范围 = (TextBox)FindControl("txt权限范围");
//                txt权限范围.Text = dt1.Rows[0]["权限范围"].ToString();
//                TextBox txt备注 = (TextBox)FindControl("txt备注");
//                txt备注.Text = dt1.Rows[0]["备注"].ToString();
//                TextBox txt联系电话 = (TextBox)FindControl("txt联系电话");
//                txt联系电话.Text = dt1.Rows[0]["联系电话"].ToString();
//                //DropDownList dll用户类型 = (DropDownList)FindControl("dll用户类型");
//                //dll用户类型.SelectedValue = dt.Rows[0]["用户类型"].ToString();
//                TextBox txt密钥编号 = (TextBox)FindControl("txt密钥编号");
//                txt密钥编号.Text = dt1.Rows[0]["密钥编号"].ToString();

//            }
            
      
            
            
//        }



       
    }
}
